<?php
/**
 * 运营项目相关
 * Created by PhpStorm.
 * User: 刘抱
 * Date: 2018/11/9
 * Time: 10:50
 */

namespace Home\Controller;


use Home\Services\CommonService;
use Home\Services\ContactService;
use Home\Services\CrmProjectService;
use Home\Services\CrmProjectsService;
use Home\Services\UserService;

class CrmProjectsController extends CommunalController
{
    //增删改操作日志记录
    public function _initialize()
    {
        new LogController();
    }

    //运营项目首页
    public function index()
    {
        $res_projects_all = CrmProjectsService::getCrmProjectsArray('*', null);//所有的运营项目
        $where = array();
        $project_id = I('project_id');
        $leader = I('leader');
        $stage = I('stage');
        if (!empty($project_id)) $where['project_id'] = $project_id;
        if (!empty($leader)) $where['leader'] = $leader;
        $where['stage'] = !empty($stage) ? $stage : 2;
        $res_projects = CrmProjectsService::getCrmProjectsArray('*', $where);
        $res_projects = CommonService::array_sort($res_projects, 'add_date');
        $count = count($res_projects);
        $Page = new \Think\Page($count, 15);
        $result = array_slice($res_projects, $Page->firstRow, $Page->listRows);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('result', $result);
        $this->assign('res_projects_all', $res_projects_all);
        $this->assign('project_id', $project_id);
        $this->assign('leader', $leader);
        $this->assign('stage', !empty($stage) ? $stage : 2);
        $this->assign('leader_array', array_unique(array_column($res_projects_all, 'leader')));
        $this->display();
    }

    //运营项目添加页
    public function add()
    {
        $projects_field = array('project_id', 'project_name');
        $projects_ids = CrmProjectsService::getCrmProjectsArray($projects_field, null);
        $projects_ids = array_column($projects_ids, 'project_id');
        $where_project = array();
        if (!empty($projects_ids)) {
            $where_project['project_id'] = array('not in', $projects_ids);//过滤已经被选择过的拓展项目
        }
        $res_crm_project = CrmProjectService::getProjectArray('project_id,name', $where_project);
        $userList = get_userName('');//所有用户
        $where_department['DEPTID'] = array('like', '13%');
        $res_department = UserService::getDepartMentArray('DEPTID,HPS_DEPT_FULLDESCR', $where_department);//部门下拉
        $res_contact = ContactService::getContactArray('contact_id,username,mobilephone', null);//负责人下拉

        if (IS_POST == true) {
            $data = $_POST;
            $db = M();
            $db->startTrans();//开启事物
            $back_data = CrmProjectsService::addCrmProjects($data);
            $flow_data = CrmProjectsService::addCrmProjectsFlow($data);
            if (!empty($back_data) && !empty($flow_data)) {
                //提交
                $db->commit();
                $this->success('添加成功', __APP__ . 'CrmProjects/index');
                die;
            } else {
                //回滚
                $db->rollback();
                $this->error('添加失败', __APP__ . 'CrmProjects/index');
                die;
            }
        }
        $this->assign('res_crm_project', $res_crm_project);
        $this->assign('res_department', $res_department);
        $this->assign('res_contact', $res_contact);
        $this->assign('user_list', $userList);
        $this->assign('province_list', $this->get_province_city());
        $this->display();
    }

    //运营项目编辑页
    public function edit()
    {
        $project_id = I('project_id');
        $where = array();
        if (!empty($project_id)) $where['project_id'] = $project_id;
        $res_projects = CrmProjectsService::getCrmProjects('*', $where);//编辑运营项目的信息
        $userList = get_userName('');//所有用户
        $res_department = UserService::getDepartMentArray('DEPTID,HPS_DEPT_FULLDESCR', null);//部门下拉
        $res_contact = ContactService::getContactArray('contact_id,username,mobilephone', null);//负责人下拉
        if (IS_POST == true) {
            $data = $_POST;
            $update = CrmProjectsService::updateCrmProjects($data, $where);
            if (!empty($update)) {
                $this->success('更新成功', __APP__ . 'CrmProjects/index');
                die;
            } else {
                $this->error('更新失败', __APP__ . 'CrmProjects/index');
                die;
            }
        }
        $this->assign('result', $res_projects);
        $this->assign('project_id', $project_id);
        $this->assign('res_department', $res_department);
        $this->assign('user_list', $userList);
        $this->assign('res_contact', $res_contact);
        $this->assign('province_list', $this->get_province_city());
        $this->display();
    }

    //首页变更状态ajax
    public function projects_flow()
    {
        $db = M();
        $db->startTrans();//开启事物
        $where['project_id'] = I('project_id');
        $update_date['stage'] = I('stage');
        $flow_data = CrmProjectsService::addCrmProjectsFlow($_POST);
        $data = CrmProjectsService::updateCrmProjects($update_date, $where);
        if (!empty($flow_data) && !empty($data)) {
            //提交
            $db->commit();
            $res['status'] = 1;
            echo json_encode($res);
            die;
        } else {
            //回滚
            $db->rollback();
            $res['status'] = false;
            $res['msg'] = '更新失败';
            echo json_encode($res);
            die;
        }
    }

    //运营项目概况页
    public function detail()
    {
        $project_id = I('project_id');
        $where['project_id'] = $project_id;
        $res_projects = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        $where_department['DEPTID'] = $res_projects['department_id'];//部门ID
        $department = UserService::getDepartMent('*', $where_department);//部门信息
        $res_projects['HPS_DEPT_FULLDESCR'] = $department['HPS_DEPT_FULLDESCR'];
        //拓展项目概况
        $res_project = CrmProjectService::getProject('*', $where);
        $res_project['type'] = explode(',', $res_project['type']);
        $province = $this->get_province_city($res_project['province_id'], 2);
        $city = $this->get_province_city($res_project['city_id'], 2);
        $res_project['address'] = $province . $city . $res_project['address'];
        //甲方信息
        $main_linkman = $res_projects['main_linkman'];
        $where_contact['contact_id'] = $main_linkman;
        $res_contact = ContactService::getContact('*', $where_contact);
        $this->assign('result', $res_projects);
        $this->assign('res_contact', $res_contact);
        $this->assign('res_project', $res_project);
        $this->display();
    }

    //运营周报列表页
    public function weekly_list()
    {
        $project_id = I('project_id');
        $where['project_id'] = array('in', $project_id);
        $res_projects = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        $this->assign('result', $res_projects);
        $res_weekly = CrmProjectsService::getWeeklyArray('*', $where, 'year desc,week desc');
        $count = count($res_weekly);
        $Page = new \Think\Page($count, 15);
        $res_weekly = array_slice($res_weekly, $Page->firstRow, $Page->listRows);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('res_weekly', $res_weekly);
        $year = date('Y');
        $this->assign('week', date('W', time()));
        $this->assign('year', $year);
        $this->display();
    }

    //运营周报添加页
    public function weekly_add()
    {
        $where['pid'] = I('pid');
        $res_projects = CrmProjectsService::getCrmProjects('*', $where);
        $this->assign("year_list", $this->get_year_month_week(1, '', ''));
        $this->assign('year', date("Y"));
        $this->assign('result', $res_projects);
        if (IS_POST == true) {
            try {
                $db = M();
                $db->startTrans();//开启事物
                $data = $_POST;
                $res = CrmProjectsService::addWeekly($data);
                if (!empty($res)) {
                    $db->commit();//提交
                    $this->success('周报添加成功', __APP__ . "CrmProjects/weekly_list/project_id/{$res_projects['project_id']}");
                    die;
                } else {
                    $db->rollback();//回滚
                    $this->error('周报添加失败', __APP__ . "CrmProjects/weekly_list/project_id/{$res_projects['project_id']}");
                    die;
                }
            } catch (\Exception $exception) {
                $db->rollback();//回滚
                $this->error('周报添加失败', __APP__ . "CrmProjects/weekly_list/project_id/{$res_projects['project_id']}");
                die;
            }
        }
        $this->display();
    }

    public function weekly_edit()
    {
        $where['weekly_id'] = I('weekly_id');
        $res_weekly = CrmProjectsService::getWeekly('*', $where);
        $where_projects['pid'] = $res_weekly['pid'];
        $res_projects = CrmProjectsService::getCrmProjects('*', $where_projects);
        $this->assign('result', $res_projects);
        $this->assign("year_list", $this->get_year_month_week(1, '', ''));
        if (IS_POST == true) {
            try {
                $db = M();
                $db->startTrans();//开启事物
                $data = $_POST;
                $where_updata['weekly_id'] = $data['weekly_id'];
                $res = CrmProjectsService::updateWeekly($data, $where_updata);
                if (!empty($res)) {
                    $db->commit();//提交
                    $this->success('周报更新成功', __APP__ . "CrmProjects/weekly_list/project_id/{$res_projects['project_id']}");
                    die;
                } else {
                    $db->rollback();//回滚
                    $this->error('周报更新失败', __APP__ . "CrmProjects/weekly_list/project_id/{$res_projects['project_id']}");
                    die;
                }
            } catch (\Exception $exception) {
                $db->rollback();//回滚
                $this->error('周报更新失败', __APP__ . "CrmProjects/weekly_list/project_id/{$res_projects['project_id']}");
                die;
            }
        }
        $this->assign('res_weekly', $res_weekly);
        $this->display();
    }

    public function weekly_del()
    {
        $project_id = I('project_id');
        $where['weekly_id'] = I('weekly_id');
        $res = CrmProjectsService::weeklyDel($where);
        if (!empty($res)) {
            $this->success('周报删除成功', __APP__ . "CrmProjects/weekly_list/project_id/$project_id");
        } else {
            $this->success('周报删除失败', __APP__ . "CrmProjects/weekly_list/project_id/$project_id");
        }
    }

    //获取一年每周的开始和结束日期
    function get_week_all($year)
    {
        $whole_week = $this->get_year_month_week(5, $year, '');
        $str = '';
        for ($i = 1; $i <= $whole_week; $i++) {
            $str .= $i . ',';
        }
        $str_arr = explode(',', rtrim($str, ','));
        $week_list = array();
        foreach ($str_arr as $key => $value) {
            $week_list[$key]['num'] = $value;
            $week = $this->get_year_month_week(6, $year, $value);
            $week_list[$key]['detail'] = "第" . $value . "周（" . $week[0] . "至" . $week[6] . "）";
        }
        echo json_encode($week_list);
    }

    //运营周报管理列表页(批复)
    public function weekly_reply()
    {
        $where = array();
        $year = I('year');
        $week = I('week');
        $add_uid = I('add_uid');
        $pid = I('pid');
        $whole_week = $this->get_year_month_week(5, $year, '');//计算选择年总共有多少周
        if (!empty($year)) $where['year'] = $year;
        if (!empty($week)) $where['week'] = $week;
        if (!empty($add_uid)) $where['add_uid'] = $add_uid;
        if (!empty($pid)) $where['pid'] = $pid;
        $res_weekly = CrmProjectsService::getWeeklyArray('*', $where, 'year desc,week desc');
        $pids = array_column($res_weekly, 'pid');
        $where_projects['pid'] = array('in', $pids);
        $res_projects = CrmProjectsService::getCrmProjectsArray('*', $where_projects);
        $res_projects_all = CrmProjectsService::getCrmProjectsArray('*', null);
        $result = CrmProjectsService::doWeeklyAndProjects($res_weekly, $res_projects);//把运营项目名称和编号组合到周报数组里
        $add_users = array_unique(array_column($result, 'add_uid'));//汇报人
        $this->assign("year_list", $this->get_year_month_week(1, '', ''));
        $this->assign('year', !empty($year) ? $year : date("Y"));
        $this->assign('week', $week);
        $this->assign('whole_week', $whole_week);
        $this->assign('add_users', $add_users);
        $this->assign('add_uid', $add_uid);
        $this->assign('res_projects_all', $res_projects_all);
        $this->assign('pid', $pid);

        $count = count($result);
        $Page = new \Think\Page($count, 15);
        $result = array_slice($result, $Page->firstRow, $Page->listRows);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('result', $result);
        $this->display();
    }

    //运营周报批复已阅ajax
    public function weekly_reply_ajax()
    {
        $where['weekly_id'] = $_POST['weekly_id'];
        $data['reply_content'] = isset($_POST['reply_content']) ? $_POST['reply_content'] : null;
        $data['reply'] = '0';
        $res = CrmProjectsService::updateWeekly($data, $where);
        if (!empty($res)) {
            $result['status'] = true;
            $result['msg'] = isset($_POST['reply_content']) ? '批复成功' : '已阅完成';
            echo json_encode($result);
            die;
        } else {
            $result['status'] = false;
            $result['msg'] = isset($_POST['reply_content']) ? '批复失败' : '已阅失败';
            echo json_encode($result);
            die;
        }
    }

    //运营日报
    public function daily()
    {
        $project_id = I('project_id');
        $where['project_id'] = $project_id;
        $project = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        $this->assign('result', $project);
        $this->assign('jysj', $this->library_type_list(7, 33, NULL, 1), 300);
        $this->assign('pid', $project['pid']);
        $this->display();
    }

    //沿用老的运营项目过程文件
    public function project_files()
    {
        $pid = I('pid');
        $where['pid'] = $pid;
        $result = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        if (!empty($_REQUEST['type'])) {
            $type = $_REQUEST['type'];
        } else {
            $type = -10;
        }
        $this->assign('type', $type);
        $this->assign('result', $result);
        //列表
        $db_project_uploads = M('db_project_uploads');
        if ($type != -10) {
            $where['db_project_uploads.type'] = $type;
        }
        $where['db_project_uploads.pid'] = $pid;
        $res_file = $db_project_uploads->where($where)->order('add_date desc')->select();
        foreach ($res_file as $key => $value) {
            $res_file[$key]['type_name'] = $this->get_db_universal_label(2, $value['type']);
            $res_file[$key]['add_user'] = $this->get_username($value['add_uid']);
            $res_file[$key]['update_user'] = $this->get_username($value['update_uid']);
        }
        //项目分类下拉菜单
        $ptype_list = $this->library_type_list(6, 24);
        $this->assign('ptype_list', $ptype_list[0]['second']);
        $this->assign('result_file', $res_file);
        $this->assign('project_bg', $this->get_project_bg($pid));
        if (empty($project['info_id'])) {
            $this->assign('qs', 'XM');
            $this->assign('manage_id', $this->login_manage_id);
        } else {
            $this->assign('qs', 'SW');
            $this->assign('manage_id', $this->login_sw_manage_id);
        }
        $this->display();

    }

    //沿用老的项目人员
    public function project_user()
    {
        //项目详情
        $pid = I('pid');
        $where['pid'] = $pid;
        $result = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        if (!empty($_REQUEST['mid'])) {
            $mid = $_REQUEST['mid'];
        }
        $this->assign('mid', $mid);
        $this->assign('result', $result);
        $db_project_user = M('db_project_user');
        $where['pid'] = $pid;
        $result_user = $db_project_user->where($where)->order("locate(position,('景区总经理,景区副总经理,景区营销副总经理,景区运营副总经理,景区营销总监,景区运营总监,景区营销副总监,景区运营副总监,景区营销高级经理,景区运营高级经理,景区营销经理,景区运营经理,景区运营主管,景区营销专员,景区运营专员,酒店总经理,酒店运营高级经理,储备干部,临时')),locate(nature,('常驻,临时'))")->select();
        foreach ($result_user as $key => $value) {
            $user = $this->get_user($value['uid']);
            $result_user[$key]['username'] = $user['username'];
            $result_user[$key]['u_status'] = $user['status'];
            $result_user[$key]['add_user'] = $this->get_username($value['add_uid']);
        }
        $this->assign('result_user', $result_user);
        //增员申请
        $db_audit_main = M('db_audit_main');
        $db_increase_detail = M('db_increase_detail');
        $where_i['pid'] = $pid;
        $where_i['type'] = 3;
        if ($_REQUEST['mid']) {
            $where_i['id'] = $_REQUEST['mid'];
        }
        $result_i = $db_audit_main->where($where_i)->order('add_date desc')->select();
        $ct = 1;
        foreach ($result_i as $key => $value) {
            if ($value['status'] != 3) {
                $ct++;
            }
            $result_i[$key]['dh_user'] = $this->get_username($value['dh_uid']);
            $audit_user = $this->audit_type_next(3, $value['audit_type'], $value['status']);
            $result_i[$key]['audit_user'] = $audit_user['audit_user'];
            $where_d['mid'] = $value['id'];
            $result_d = $db_increase_detail->where($where_d)->select();
            $result_i[$key]['detail'] = $result_d;
            $result_i[$key]['sum'] = count($result_d);
            //审核人员
            $audit_arr = $this->audit_user(3, $value['audit_type']);
            foreach ($audit_arr as $kk => $vv) {
                if ($value['status'] > $vv['audit_sort']) {
                    switch ($vv['audit_name']) {
                        case '审核人':
                            $audit_arr[$kk]['audit_user'] = $this->get_username($value['audit1_uid']);
                            $audit_arr[$kk]['audit_date'] = $value['audit1_date'];
                            $audit_arr[$kk]['audit_comments'] = $value['audit1_message'];
                            break;
                        case '审批人':
                            $audit_arr[$kk]['audit_user'] = $this->get_username($value['audit2_uid']);
                            $audit_arr[$kk]['audit_date'] = $value['audit2_date'];
                            $audit_arr[$kk]['audit_comments'] = $value['audit2_message'];
                            break;
                    }
                }
                if (empty($vv['audit_uid']) and empty($audit_arr[$kk]['audit_user'])) {
                    unset($audit_arr[$kk]);
                }
            }
            $array2 = array("audit_name" => "申请人", "audit_user" => "" . $this->get_username($value['post_uid']) . "", "audit_date" => "" . $value['post_date'] . "", "audit_sort" => "-1");
            array_unshift($audit_arr, $array2);
            foreach ($audit_arr as $k1 => $v1) {
                if ($value['arrive_status'] == $v1['audit_sort']) {
                    $arrive_name = $v1['audit_name'];
                    if ($value['arrive_status'] == '-1') {
                        $arrive_info = $value['submit_info'];
                    } else {
                        $arrive_info = $v1['audit_comments'];
                    }
                    $arrive_date = $v1['audit_date'];
                }
                if ($value['dh_status'] == $v1['audit_sort']) {
                    $audit_arr[$k1]['arrive_name'] = $arrive_name;
                    $audit_arr[$k1]['dh_message'] = $value['dh_message'];
                    $audit_arr[$k1]['dh_date'] = $value['dh_date'];
                    $audit_arr[$k1]['arrive_info'] = $arrive_info;
                    $audit_arr[$k1]['arrive_date'] = $arrive_date;
                }
            }
            $result_i[$key]['audit_arr'] = $audit_arr;
            if ($_REQUEST['mid']) {
                $where_de['mid'] = $value['id'];
                $detail = $db_increase_detail->where($where_de)->select();
                $this->assign('sum', count($detail));
                $this->assign('detail', $detail);
                $this->assign('audit_user', $audit_arr);
                foreach ($audit_arr as $ka => $va) {
                    if ($value['status'] <= $va['audit_sort']) {
                        unset($audit_arr[$ka]);
                    }
                }
                $this->assign('audit_list', $audit_arr);
                $audit_sh = $this->audit_type_next_person(3, $value['audit_type'], $value['status'], 'egt');
                $audit_qs = $this->audit_type_next_person(3, $value['audit_type'], $value['status'], 'lt');
                if (in_array($this->login_id, explode(',', $audit_sh['audit_uid']))) {
                    $shenhe = 1;
                }
                if (in_array($this->login_id, explode(',', $audit_qs['audit_uid']))) {
                    $qishen = 1;
                }
                $this->assign('shenhe', $shenhe);
                $this->assign('qishen', $qishen);
            }
        }
        $this->assign('ct', $ct);
        $this->assign('result_i', $result_i);
        $this->assign('project_bg', $this->get_project_bg($pid));
        //职位下拉菜单
        $db_universal_label2 = M('db_universal_label2');
        $where_s['first_id'] = 8;
        $where_s['second_name'] = array('in', '景区总经理,景区副总经理,景区营销副总经理,景区运营副总经理,景区营销总监,景区运营总监,景区营销副总监,景区运营副总监,景区营销高级经理,景区运营高级经理,景区营销经理,景区运营经理,景区运营主管,景区营销专员,景区运营专员,酒店总经理,酒店运营高级经理,储备干部,临时');
        $position = $db_universal_label2->field('id,second_name')->where($where_s)->order("locate(second_name,('景区总经理,景区副总经理,景区营销副总经理,景区运营副总经理,景区营销总监,景区运营总监,景区营销副总监,景区运营副总监,景区营销高级经理,景区运营高级经理,景区营销经理,景区运营经理,景区运营主管,景区营销专员,景区运营专员,酒店总经理,酒店运营高级经理,储备干部,临时'))")->select();
        $this->assign('result4', $position);
        //职级下拉菜单
        $where_z['first_id'] = 35;
        $where_z['second_name'] = array('in', 'B4,B5,B6,B7,B8,B9,B10');
        $rank = $db_universal_label2->field('id,second_name')->where($where_z)->order("locate(second_name,('B4,B5,B6,B7,B8,B9,B10'))")->select();
        $this->assign('result5', $rank);
        if (empty($project['info_id'])) {
            $this->assign('qs', 'XM');
            $this->assign('manage_id', $this->login_manage_id);
        } else {
            $this->assign('qs', 'SW');
            $this->assign('manage_id', $this->login_sw_manage_id);
        }
        $this->display();
    }

    //沿用老的经营数据分析
    public function project_operate_analysis()
    {
        if (!empty($_REQUEST['types'])) {
            $types = $_REQUEST['types'];
        } else {
            $types = '图表';
        }
        $this->assign('types', $types);
        if (!empty($_REQUEST['mon'])) {
            $mon = $_REQUEST['mon'];
        } else {
            $mon = '全部收入';
        }
        $this->assign('mon', $mon);
        if (!empty($_REQUEST['mode'])) {
            $mode = $_REQUEST['mode'];
        } else {
            $mode = '折叠';
        }
        $this->assign('mode', $mode);
        //项目详情
        $where['pid'] = $_REQUEST['pid'];
        $result = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        $this->assign('result', $result);
        //项目启动表
        $project_bg = $this->get_project_bg($_REQUEST['pid']);
        $this->assign('project_bg', $project_bg);
        //统计
        $db_project_operate = M('db_project_operate');
        $db_project_operate_detail = M('db_project_operate_detail');
        $db_project_operate_data = M('db_project_operate_data');
        $where_sam['pid'] = $_REQUEST['pid'];
        $where_sam['type'] = 1;
        $data = $db_project_operate->where($where_sam)->order('year,week')->select();
        if (count($data) <= 8) {
            $star = 0;
        } else {
            $star = 8 / count($data) * 100;
        }
        $this->assign('star', $star);
        if ($mon == '全部收入' or $types == '列表') {
            foreach ($data as $ka => $va) {
                unset($where_d['tid']);
                unset($where_d['sid']);
                $vaa = $this->get_year_month_week(6, $va['year'], $va['week']);
                if ($va['year_week'] < "201804") {
                    $where_d['mid'] = $va['id'];
                    $where_d['fid'] = 33;
                    $sjy = $db_project_operate_detail->where($where_d)->group('sid')->order('tid')->select();
                    $hj_sk_p = 0;
                    $hj_sk_m = 0;
                    $hj_td_p = 0;
                    $hj_td_m = 0;
                    $hj_p = 0;
                    $hj_m = 0;
                    $hj_ec = 0;
                    foreach ($sjy as $kk => $vv) {
                        unset($where_d['tid']);
                        $where_d['sid'] = $vv['sid'];
                        if ($vv['sid'] == 254) {
                            $mps = $db_project_operate_detail->where($where_d)->group('tid')->order('tid')->select();
                            foreach ($mps as $k1 => $v1) {
                                $where_d['tid'] = $v1['tid'];
                                $type = $db_project_operate_detail->where($where_d)->order('people desc')->select();
                                foreach ($type as $k3 => $v3) {
                                    if ($v3['people'] == '散客') {
                                        $hj_sk_p += $v3['number'];
                                        $hj_sk_m += $v3['money'];
                                    } elseif ($v3['people'] == '团队') {
                                        $hj_td_p += $v3['number'];
                                        $hj_td_m += $v3['money'];
                                    }
                                    $hj_p += $v3['number'];
                                    $hj_m += $v3['money'];
                                }
                            }
                        } elseif ($vv['sid'] == 255) {
                            unset($where_d['tid']);
                            $ecs = $db_project_operate_detail->where($where_d)->order('tid')->select();
                            foreach ($ecs as $k2 => $v2) {
                                $hj_ec += $v2['money'];
                            }
                        }
                    }
                } else {
                    $where_a['date'] = array(between, array($vaa[0], $vaa[6]));
                    $where_a['pid'] = $_REQUEST['pid'];
                    $where_a['fid'] = 33;
                    $where_a['sid'] = 254;
                    $operate_m = $db_project_operate_data->field('sum(money) as money')->where($where_a)->find();
                    $hj_m = $operate_m['money'] - 0;
                    $where_a['sid'] = 255;
                    $operate_e = $db_project_operate_data->field('sum(money) as money')->where($where_a)->find();
                    $hj_ec = $operate_e['money'] - 0;
                }
                if ($ka % 2 == 1) {
                    $weeks[] = "'\\n第" . $va['week'] . "周(" . substr($vaa[0], 5, 2) . '.' . substr($vaa[0], 8, 2) . "-" . substr($vaa[6], 5, 2) . '.' . substr($vaa[6], 8, 2) . ")'";
                    $all[$ka]['week'] = $va['year'] . "年 " . "第" . $va['week'] . "周（" . $vaa[0] . "至" . $vaa[6] . "）";
                } else {
                    $weeks[] = "'第" . $va['week'] . "周(" . substr($vaa[0], 5, 2) . '.' . substr($vaa[0], 8, 2) . "-" . substr($vaa[6], 5, 2) . '.' . substr($vaa[6], 8, 2) . ")'";
                    $all[$ka]['week'] = $va['year'] . "年 " . "第" . $va['week'] . "周（" . $vaa[0] . "至" . $vaa[6] . "）";
                }
                $sums[] = $hj_ec + $hj_m;
                $mp_hjs[] = $hj_m;
                $ec_hjs[] = $hj_ec;
                $all[$ka]['all_sum'] = $hj_ec + $hj_m;
                $all[$ka]['mp_sum'] = $hj_m;
                $all[$ka]['ec_sum'] = $hj_ec;
            }
            $this->assign('color', "'#fb607f','#4680fe','#8d81c9'");
            $this->assign('legend', "'总收入','门票金额','二消金额'");
            $sumss[] = join(',', $sums);
            $sumss[] = join(',', $mp_hjs);
            $sumss[] = join(',', $ec_hjs);
            $name[] = '总收入';
            $name[] = '门票金额';
            $name[] = '二消金额';
        }
        if ($mon == '门票' or $types == '列表') {
            $name[] = '总门票金额';
            $db_universal_label3 = M('db_universal_label3');
            $where['first_id'] = 33;
            $where['second_id'] = 254;
            $type3 = $db_universal_label3->where($where)->select();
            foreach ($data as $ka => $va) {
                $mp_sum = 0;
                $typesa = 0;
                $vaa = $this->get_year_month_week(6, $va['year'], $va['week']);
                foreach ($type3 as $k3 => $v3) {
                    $typea = 0;
                    if ($va['year_week'] < "201804") {
                        $where_dm['mid'] = $va['id'];
                        $where_dm['tid'] = $v3['id'];
                        $mps = $db_project_operate_detail->where($where_dm)->select();
                        foreach ($mps as $k5 => $v5) {
                            if ($mps['people'] == '团队') {
                                $typea += $v5['money'];
                            } else {
                                $typea += $v5['money'];
                            }
                            $mp_sum += $v5['money'];
                        }
                        $mp_sums[$k3 + 1][] = $typea;
                        if (!empty($typea)) {
                            $all[$ka]['detail'][$typesa]['mp_name'] = $v3['third_name'];
                            $all[$ka]['detail'][$typesa]['mp_sum'] = $typea;
                            $typesa++;
                            $all[$ka]['mp_max'] = $typesa;
                        }
                        $name[$k3 + 1] = $v3['third_name'];
                    } else {
                        $where_dm['date'] = array(between, array($vaa[0], $vaa[6]));
                        $where_dm['pid'] = $_REQUEST['pid'];
                        $where_dm['tid'] = $v3['id'];
                        $mps = $db_project_operate_data->where($where_dm)->select();
                        foreach ($mps as $k5 => $v5) {
                            if ($mps['people'] == '团队') {
                                $typea += $v5['money'];
                            } else {
                                $typea += $v5['money'];
                            }
                            $mp_sum += $v5['money'];
                        }
                        $mp_sums[$k3 + 1][] = $typea;
                        if (!empty($typea)) {
                            $all[$ka]['detail'][$typesa]['mp_name'] = $v3['third_name'];
                            $all[$ka]['detail'][$typesa]['mp_sum'] = $typea;
                            $typesa++;
                            $all[$ka]['mp_max'] = $typesa;
                        }
                        $name[$k3 + 1] = $v3['third_name'];
                    }
                }
                $mp_sums[0][] = $mp_sum;
                $vaa = $this->get_year_month_week(6, $va['year'], $va['week']);
                if ($ka % 2 == 1) {
                    $weeks[] = "'\\n第" . $va['week'] . "周(" . substr($vaa[0], 5, 2) . '.' . substr($vaa[0], 8, 2) . "-" . substr($vaa[6], 5, 2) . '.' . substr($vaa[6], 8, 2) . ")'";
                } else {
                    $weeks[] = "'第" . $va['week'] . "周(" . substr($vaa[0], 5, 2) . '.' . substr($vaa[0], 8, 2) . "-" . substr($vaa[6], 5, 2) . '.' . substr($vaa[6], 8, 2) . ")'";
                }
            }
            ksort($mp_sums);
            foreach ($mp_sums as $kk => $vv) {
                $sumss[$kk] = join(',', $vv);
            }
            $this->assign('color', "'#f49900','#fb607f','#4680fe','#a9d26a','#8d81c9'");
            $this->assign('legend', "'总门票金额','全价票','优惠票','渠道活动票','其它'");
        }
        if ($mon == '二消' or $types == '列表') {
            $name[] = '总二消金额';
            $db_universal_label3 = M('db_universal_label3');
            $where['first_id'] = 33;
            $where['second_id'] = 255;
            $type3 = $db_universal_label3->where($where)->select();
            foreach ($data as $ka => $va) {
                $ec_sum = 0;
                $typesb = 0;
                $vaa = $this->get_year_month_week(6, $va['year'], $va['week']);
                foreach ($type3 as $k3 => $v3) {
                    $typeb = 0;
                    if ($va['year_week'] < "201804") {
                        $where_de['mid'] = $va['id'];
                        $where_de['tid'] = $v3['id'];
                        $mps = $db_project_operate_detail->where($where_de)->select();
                        foreach ($mps as $k5 => $v5) {
                            if ($mps['people'] == '团队') {
                                $typeb += $v5['money'];
                            } else {
                                $typeb += $v5['money'];
                            }
                            $ec_sum += $v5['money'];
                        }
                        $ec_sums[$k3 + 1][] = $typeb;
                        if (!empty($typeb)) {
                            $all[$ka]['detail'][$typesb]['ec_name'] = $v3['third_name'];
                            $all[$ka]['detail'][$typesb]['ec_sum'] = $typeb;
                            $typesb++;
                            $all[$ka]['ec_max'] = $typesb;
                        }
                        $name[$k3 + 1] = $v3['third_name'];
                    } else {
                        $where_de['date'] = array(between, array($vaa[0], $vaa[6]));
                        $where_de['pid'] = $_REQUEST['pid'];
                        $where_de['tid'] = $v3['id'];
                        $mps = $db_project_operate_data->where($where_de)->select();
                        foreach ($mps as $k5 => $v5) {
                            if ($mps['people'] == '团队') {
                                $typeb += $v5['money'];
                            } else {
                                $typeb += $v5['money'];
                            }
                            $ec_sum += $v5['money'];
                        }
                        $ec_sums[$k3 + 1][] = $typeb;
                        if (!empty($typeb)) {
                            $all[$ka]['detail'][$typesb]['ec_name'] = $v3['third_name'];
                            $all[$ka]['detail'][$typesb]['ec_sum'] = $typeb;
                            $typesb++;
                            $all[$ka]['ec_max'] = $typesb;
                        }
                        $name[$k3 + 1] = $v3['third_name'];
                    }
                }
                $ec_sums[0][] = $ec_sum;
                if ($ka % 2 == 1) {
                    $weeks[] = "'\\n第" . $va['week'] . "周(" . substr($vaa[0], 5, 2) . '.' . substr($vaa[0], 8, 2) . "-" . substr($vaa[6], 5, 2) . '.' . substr($vaa[6], 8, 2) . ")'";
                } else {
                    $weeks[] = "'第" . $va['week'] . "周(" . substr($vaa[0], 5, 2) . '.' . substr($vaa[0], 8, 2) . "-" . substr($vaa[6], 5, 2) . '.' . substr($vaa[6], 8, 2) . ")'";
                }
            }
            ksort($ec_sums);
            foreach ($ec_sums as $kk => $vv) {
                $sumss[$kk] = join(',', $vv);
            }
            $this->assign('color', "'#f49900','#fb607f','#f06ecd ','#8d81c9','#4680fe','#8fe2ce','#a9d26a'");
            $this->assign('legend', "'总二消金额','停车','特色交通','导游','餐饮','园中园','其它'");
        }
        if ($mon == '成本' or $types == '列表') {
            $name[] = '总成本';
            $db_universal_label3 = M('db_universal_label3');
            $where['first_id'] = 33;
            $where['second_id'] = 496;
            $type3 = $db_universal_label3->where($where)->select();
            foreach ($data as $ka => $va) {
                $cb_sum = 0;
                $typesb = 0;
                $vaa = $this->get_year_month_week(6, $va['year'], $va['week']);
                foreach ($type3 as $k3 => $v3) {
                    $typeb = 0;
                    if ($va['year_week'] < "201804") {
                        $where_de['mid'] = $va['id'];
                        $where_de['tid'] = $v3['id'];
                        $mps = $db_project_operate_detail->where($where_de)->select();
                        foreach ($mps as $k5 => $v5) {
                            if ($mps['people'] == '团队') {
                                $typeb += $v5['money'];
                            } else {
                                $typeb += $v5['money'];
                            }
                            $cb_sum += $v5['money'];
                        }
                        $cb_sums[$k3 + 1][] = $typeb;
                        if (!empty($typeb)) {
                            $all[$ka]['detail'][$typesb]['cb_name'] = $v3['third_name'];
                            $all[$ka]['detail'][$typesb]['cb_sum'] = $typeb;
                            $typesb++;
                            $all[$ka]['cb_max'] = $typesb;
                        }
                        $name[$k3 + 1] = $v3['third_name'];
                    } else {
                        $where_de['date'] = array(between, array($vaa[0], $vaa[6]));
                        $where_de['pid'] = $_REQUEST['pid'];
                        $where_de['tid'] = $v3['id'];
                        $mps = $db_project_operate_data->where($where_de)->select();
                        foreach ($mps as $k5 => $v5) {
                            if ($mps['people'] == '团队') {
                                $typeb += $v5['money'];
                            } else {
                                $typeb += $v5['money'];
                            }
                            $cb_sum += $v5['money'];
                        }
                        $cb_sums[$k3 + 1][] = $typeb;
                        if (!empty($typeb)) {
                            $all[$ka]['detail'][$typesb]['cb_name'] = $v3['third_name'];
                            $all[$ka]['detail'][$typesb]['cb_sum'] = $typeb;
                            $typesb++;
                            $all[$ka]['cb_max'] = $typesb;
                        }
                        $name[$k3 + 1] = $v3['third_name'];
                    }
                }
                $cb_sums[0][] = $cb_sum;
                if ($ka % 2 == 1) {
                    $weeks[] = "'\\n第" . $va['week'] . "周(" . substr($vaa[0], 5, 2) . '.' . substr($vaa[0], 8, 2) . "-" . substr($vaa[6], 5, 2) . '.' . substr($vaa[6], 8, 2) . ")'";
                } else {
                    $weeks[] = "'第" . $va['week'] . "周(" . substr($vaa[0], 5, 2) . '.' . substr($vaa[0], 8, 2) . "-" . substr($vaa[6], 5, 2) . '.' . substr($vaa[6], 8, 2) . ")'";
                }
            }
            ksort($cb_sums);
            foreach ($cb_sums as $kk => $vv) {
                $sumss[$kk] = join(',', $vv);
            }
            $this->assign('color', "'#f49900','#fb607f','#f06ecd ','#8d81c9','#4680fe','#8fe2ce','#a9d26a'");
            $this->assign('legend', "'总成本','经营成本','营销费用','管理费用'");
        }
        $this->assign('weeks', join(',', $weeks));
        $this->assign('name', $name);
        $this->assign('name_sum', count($name));
        $this->assign('sumss', $sumss);
        sort($all);
        $this->assign('all', $all);
        if (empty($project['info_id'])) {
            $this->assign('qs', 'XM');
            $this->assign('manage_id', $this->login_manage_id);
        } else {
            $this->assign('qs', 'SW');
            $this->assign('manage_id', $this->login_sw_manage_id);
        }
        $this->display();
    }

    //沿用老的月计划
    public function project_income_month()
    {
        //项目详情
        $where['pid'] = $_REQUEST['pid'];
        $result = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        $this->assign('result', $result);
        $this->assign('project_bg', $this->get_project_bg($_REQUEST['pid']));
        $year = $_REQUEST['year'] ? $_REQUEST['year'] : date("Y");
        $this->assign('year', $year);
        $this->assign("year_list", $this->get_year_month_week(1));
        $result_jh = $this->get_db_universal_type(15);
        $db_project_income_month = M('db_project_income_month');
        $db_project_operate = M('db_project_operate');
        $db_project_operate_detail = M('db_project_operate_detail');
        $db_project_operate_data = M('db_project_operate_data');
        $where_sam['pid'] = $_REQUEST['pid'];
        $where_sam['type'] = 1;
        $data = $db_project_operate->where($where_sam)->order('year,week')->select();
        foreach ($result_jh as $key => $value) {
            $where['pid'] = $_REQUEST['pid'];
            $where['year'] = $year;
            $where['tid'] = $value['name1'];
            $result_income = $db_project_income_month->where($where)->find();
            $result_jh[$key]['y_sum'] = $result_income['y_sum'];
            $result_jh[$key]['m1'] = $result_income['m1'];
            $result_jh[$key]['m2'] = $result_income['m2'];
            $result_jh[$key]['m3'] = $result_income['m3'];
            $result_jh[$key]['m4'] = $result_income['m4'];
            $result_jh[$key]['m5'] = $result_income['m5'];
            $result_jh[$key]['m6'] = $result_income['m6'];
            $result_jh[$key]['m7'] = $result_income['m7'];
            $result_jh[$key]['m8'] = $result_income['m8'];
            $result_jh[$key]['m9'] = $result_income['m9'];
            $result_jh[$key]['m10'] = $result_income['m10'];
            $result_jh[$key]['m11'] = $result_income['m11'];
            $result_jh[$key]['m12'] = $result_income['m12'];
            $where_a = NULL;
            $where_a['pid'] = $_REQUEST['pid'];
            $where_a['fid'] = 33;
            $where_a['date'] = array(between, array($year . '-01-01', $year . '-12-31'));
            if ($value['name1'] == 1) {
                $operate_m = $db_project_operate_data->field('number as money,date')->where($where_a)->select();
            } elseif ($value['name1'] == 2) {
                $where_a['_string'] = 'sid=254 or sid=255';
                $operate_m = $db_project_operate_data->where($where_a)->select();
            } elseif ($value['name1'] == 3) {
                $where_a['sid'] = 496;
                $where_a['tid'] = 87;
                $operate_m = $db_project_operate_data->where($where_a)->select();
            } elseif ($value['name1'] == 4) {
                $where_a['sid'] = 496;
                $where_a['tid'] = 88;
                $operate_m = $db_project_operate_data->where($where_a)->select();
            } elseif ($value['name1'] == 5) {
                $where_a['sid'] = 496;
                $where_a['tid'] = 89;
                $operate_m = $db_project_operate_data->where($where_a)->select();
            }
            $n1 = NULL;
            $n2 = NULL;
            $n3 = NULL;
            $n4 = NULL;
            $n5 = NULL;
            $n6 = NULL;
            $n7 = NULL;
            $n8 = NULL;
            $n9 = NULL;
            $n10 = NULL;
            $n11 = NULL;
            $n12 = NULL;
            $n_sum = NULL;
            foreach ($operate_m as $kk => $vv) {
                $n_sum += $vv['money'];
                if (substr($vv['date'], 5, 2) == '01') {
                    $n1 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '02') {
                    $n2 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '03') {
                    $n3 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '04') {
                    $n4 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '05') {
                    $n5 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '06') {
                    $n6 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '07') {
                    $n7 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '08') {
                    $n8 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '09') {
                    $n9 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '10') {
                    $n10 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '11') {
                    $n11 += $vv['money'];
                }
                if (substr($vv['date'], 5, 2) == '12') {
                    $n12 += $vv['money'];
                }
            }
            $result_jh[$key]['n_sum'] = round($n_sum / 10000, 2);
            $result_jh[$key]['n_sum_bl'] = round(($n_sum / ($result_income['y_sum'] * 10000)) * 100, 2);
            $result_jh[$key]['n1'] = round($n1 / 10000, 2);
            $result_jh[$key]['n1_bl'] = round(($n1 / ($result_income['m1'] * 10000)) * 100, 2);
            $result_jh[$key]['n2'] = round($n2 / 10000, 2);
            $result_jh[$key]['n2_bl'] = round(($n2 / ($result_income['m2'] * 10000)) * 100, 2);
            $result_jh[$key]['n3'] = round($n3 / 10000, 2);
            $result_jh[$key]['n3_bl'] = round(($n3 / ($result_income['m3'] * 10000)) * 100, 2);
            $result_jh[$key]['n4'] = round($n4 / 10000, 2);
            $result_jh[$key]['n4_bl'] = round(($n4 / ($result_income['m4'] * 10000)) * 100, 2);
            $result_jh[$key]['n5'] = round($n5 / 10000, 2);
            $result_jh[$key]['n5_bl'] = round(($n5 / ($result_income['m5'] * 10000)) * 100, 2);
            $result_jh[$key]['n6'] = round($n6 / 10000, 2);
            $result_jh[$key]['n6_bl'] = round(($n6 / ($result_income['m6'] * 10000)) * 100, 2);
            $result_jh[$key]['n7'] = round($n7 / 10000, 2);
            $result_jh[$key]['n7_bl'] = round(($n7 / ($result_income['m7'] * 10000)) * 100, 2);
            $result_jh[$key]['n8'] = round($n8 / 10000, 2);
            $result_jh[$key]['n8_bl'] = round(($n8 / ($result_income['m8'] * 10000)) * 100, 2);
            $result_jh[$key]['n9'] = round($n9 / 10000, 2);
            $result_jh[$key]['n9_bl'] = round(($n9 / ($result_income['m9'] * 10000)) * 100, 2);
            $result_jh[$key]['n10'] = round($n10 / 10000, 2);
            $result_jh[$key]['n10_bl'] = round(($n10 / ($result_income['m10'] * 10000)) * 100, 2);
            $result_jh[$key]['n11'] = round($n11 / 10000, 2);
            $result_jh[$key]['n11_bl'] = round(($n11 / ($result_income['m11'] * 10000)) * 100, 2);
            $result_jh[$key]['n12'] = round($n12 / 10000, 2);
            $result_jh[$key]['n12_bl'] = round(($n12 / ($result_income['m12'] * 10000)) * 100, 2);
        }
        if ($_REQUEST['action'] == 'add') {
            foreach ($_REQUEST['tid'] as $key => $value) {
                $where['pid'] = $_REQUEST['pid'];
                $where['year'] = $_REQUEST['year'];
                $where['tid'] = $value;
                $result_income = $db_project_income_month->where($where)->find();
                if ($result_income) {
                    $where_save['id'] = $result_income['id'];
                    $where_save['y_sum'] = $_REQUEST['m' . $value . 'y_sum'];
                    $where_save['m1'] = $_REQUEST['m' . $value . 'm1'];
                    $where_save['m2'] = $_REQUEST['m' . $value . 'm2'];
                    $where_save['m3'] = $_REQUEST['m' . $value . 'm3'];
                    $where_save['m4'] = $_REQUEST['m' . $value . 'm4'];
                    $where_save['m5'] = $_REQUEST['m' . $value . 'm5'];
                    $where_save['m6'] = $_REQUEST['m' . $value . 'm6'];
                    $where_save['m7'] = $_REQUEST['m' . $value . 'm7'];
                    $where_save['m8'] = $_REQUEST['m' . $value . 'm8'];
                    $where_save['m9'] = $_REQUEST['m' . $value . 'm9'];
                    $where_save['m10'] = $_REQUEST['m' . $value . 'm10'];
                    $where_save['m11'] = $_REQUEST['m' . $value . 'm11'];
                    $where_save['m12'] = $_REQUEST['m' . $value . 'm12'];
                    $db_project_income_month->save($where_save);
                } else {
                    $where_add['pid'] = $_REQUEST['pid'];
                    $where_add['year'] = $_REQUEST['year'];
                    $where_add['tid'] = $value;
                    $where_add['y_sum'] = $_REQUEST['m' . $value . 'y_sum'];
                    $where_add['m1'] = $_REQUEST['m' . $value . 'm1'];
                    $where_add['m2'] = $_REQUEST['m' . $value . 'm2'];
                    $where_add['m3'] = $_REQUEST['m' . $value . 'm3'];
                    $where_add['m4'] = $_REQUEST['m' . $value . 'm4'];
                    $where_add['m5'] = $_REQUEST['m' . $value . 'm5'];
                    $where_add['m6'] = $_REQUEST['m' . $value . 'm6'];
                    $where_add['m7'] = $_REQUEST['m' . $value . 'm7'];
                    $where_add['m8'] = $_REQUEST['m' . $value . 'm8'];
                    $where_add['m9'] = $_REQUEST['m' . $value . 'm9'];
                    $where_add['m10'] = $_REQUEST['m' . $value . 'm10'];
                    $where_add['m11'] = $_REQUEST['m' . $value . 'm11'];
                    $where_add['m12'] = $_REQUEST['m' . $value . 'm12'];
                    $db_project_income_month->add($where_add);
                }
            }
            $data['status'] = parent::YES;
            echo json_encode($data);
            die;
        }
        $this->assign('result_jh', $result_jh);
        if (empty($project['info_id'])) {
            $this->assign('qs', 'XM');
            $this->assign('manage_id', $this->login_manage_id);
        } else {
            $this->assign('qs', 'SW');
            $this->assign('manage_id', $this->login_sw_manage_id);
        }
        $this->display();
    }

}